import os
import tornado
from whirlwind.db.mongo import Mongo
from tornado.web import authenticated
from tornado.web import asynchronous
from tornado.locale import CSVLocale
from tornado.options import options
from whirlwind.view.decorators import route
from pytz import all_timezones
from HandlerExtension.BasicHandler import BasicHandler
from application.models.user import User

@route('/profile')
class ProfileHandler(BasicHandler):
    @authenticated
    def get(self):
        #template context variables go in here
        template_values = {}
       
        user = self.get_current_user()
        self.nonactive_behavior(user)

        default_imglink = str(options.domain) + ':' + str(options.port) + '/static/img/soy.jpg'
        template_values['username'] = user.username
        template_values['myimg'] = user.get_imagelink(200, default_imglink)
        template_values['mylocale'] = user.profile.locale
        template_values['mytimezone'] = user.profile.timezone
        template_values['level'] = user.profile.level
        template_values['credit'] = user.profile.credit
        template_values['interests'] = user.profile.interests
        self.render_template('/profile/profile.html',**template_values)
        

@route('/profile/edit')
class ProfileEditHandler(BasicHandler):
    @authenticated
    def get(self):
        template_values = {}

        user = self.get_current_user()
        self.nonactive_behavior(user)

        default_imglink = str(options.domain) + ':' + str(options.port) + '/static/img/soy.jpg'

        template_values['username'] = user.username
        template_values['myimg'] = user.get_imagelink(200, default_imglink)
        template_values['level'] = user.profile.level
        template_values['credit'] = user.profile.credit
        template_values['mylocale'] = user.profile.locale
        template_values['mytimezone'] = user.profile.timezone
        template_values['level'] = user.profile.level
        template_values['interests'] = user.profile.interests
        template_values['locale_list'] = tornado.locale.get_supported_locales(CSVLocale)
        template_values['timezone_list'] = all_timezones
        self.render_template('/profile/edit_profile.html',**template_values)

    @authenticated
    def post(self):
        user = self.get_current_user()
        self.nonactive_behavior(user)
        username = self.get_argument('username')
        if len(username) < 4:
            self.flash.error = "Invalid user name! The username is too short."
            self.redirect('/profile/edit')
        user.username = username
        user.profile.locale = self.get_argument('locale', user.profile.locale)
        user.profile.timezone = self.get_argument('timezone', user.profile.timezone)
        user.save()
        self.flash.success = "Successful update your profile!"
        self.redirect('/profile')
      

@route("/profile/addapi")
class AddAPIHandler(BasicHandler):
    @authenticated
    def get(self):
        template_values = {}
        user = self.get_current_user()
        template_values['username'] = user.username
        template_values['mykeys'] = [key['name'] for key in user.mykeys]
        apis = tornado.escape.json_decode(open(os.path.join(options.config_dir, "api.json")).read())
        apikeys = apis.keys()
        for key in template_values['mykeys']:
            apikeys.remove(key)
        template_values['available_keys'] = apikeys
        print template_values
        self.render_template('/profile/add_api.html', **template_values)
